package com.pengtu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.dao.project.ProjectInfoDao;
import com.pengtu.dao.quality.FlowProjectDao;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.entity.quality.FlowProject;
import com.pengtu.service.JbpmTemplateManager;

/**
 * 项目流程实体相关的管理类
 * 
 * @author liuyang
 * 
 */
@Component
@Transactional
public class FProjectManager {
	private FlowProjectDao flowProjectDao;
	private JbpmTemplateManager jbpmTemplateManager;
	private ProjectInfoDao projectDao;

	@Autowired
	public void setJbpmTemplateManager(JbpmTemplateManager jbpmTemplateManager) {
		this.jbpmTemplateManager = jbpmTemplateManager;
	}

	@Autowired
	public void setFlowProjectDao(FlowProjectDao flowProjectDao) {
		this.flowProjectDao = flowProjectDao;
	}

	@Autowired
	public void setProjectDao(ProjectInfoDao projectDao) {
		this.projectDao = projectDao;
	}

	/**
	 * 根据ID查询
	 * 
	 * @param id
	 * @return
	 */
	public FlowProject get(String id) {
		return flowProjectDao.get(id);
	}

	/**
	 * 部署流程到数据库
	 * 
	 * @param resourceName
	 * @param entity
	 */
	public void deploy(String resourceName, FlowProject entity) {
		String deploymentId = jbpmTemplateManager.deploy(resourceName);
		entity.setDeploymentId(deploymentId);
		entity.setStatus("0");
		if (flowProjectDao.findUniqueBy("key", entity.getKey()) == null) {
			flowProjectDao.save(entity);
		}else{
			flowProjectDao.delete(flowProjectDao.findUniqueBy("key", entity.getKey()));
			flowProjectDao.save(entity);
		}
	}

	/**
	 * 根据key查询
	 * 
	 * @param key
	 * @return
	 */
	public FlowProject getByKey(String key) {
		return flowProjectDao.findUniqueBy("key", key);
	}

	/**
	 * 查询项目状态
	 * 
	 * @param projId
	 * @return
	 */
	public String getById(String projId) {
		return projectDao.get(projId).getStatus();
	}

	/**
	 * 查询项目
	 * 
	 * @param id
	 * @return
	 */
	public ProjectInfo find(String id) {
		return projectDao.get(id);
	}
}
